package com.admin.controller;


import com.admin.entity.Announcement;
import com.admin.entity.Document;
import com.admin.service.AnnouncementService;
import com.admin.service.DocumentService;
import com.admin.utils.Result;
import com.admin.vo.query.AnnouncementQueryVo;
import com.admin.vo.query.DocumentQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author szy
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/api/document")
public class DocumentController {

    @Resource
    private DocumentService documentService;

    /*分页查询*/
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:material:select')")
    public Result list(DocumentQueryVo documentQueryVo){
        IPage<Document> page = new Page<Document>(documentQueryVo.getPageNo(),documentQueryVo.getPageSize());
        documentService.getDocumentByPage(page,documentQueryVo);
        return Result.ok(page);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:material:add')")
    public Result add(@RequestBody Document document){
        if (documentService.save(document)){
            return Result.ok().message("添加成功");
        }else
            return Result.error().message("添加失败");
    }

    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:material:edit')")
    public Result update(@RequestBody Document document){
        if (documentService.updateById(document)){
            return Result.ok().message("修改成功");
        }else
            return Result.error().message("修改失败");
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:material:delete')")
    public Result delete(@PathVariable Integer id){
        if (documentService.removeById(id)){
            return Result.ok().message("删除成功");
        }else
            return Result.error().message("删除失败");
    }

}

